package com.igoal.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.igoal.entity.DataStatus;
import com.igoal.entity.Message;

@Repository("messageDAO")
public class MessageDAOImpl extends GenericDaoHibernate<Message, Long>
		implements MessageDAO {

	public MessageDAOImpl() {
		super(Message.class);
	}
	
	public List<Message> getMessage(Long userId, boolean read){
		DetachedCriteria dc = DetachedCriteria.forClass(Message.class);
		dc.add(Restrictions.eq("to.id", userId));
		dc.add(Restrictions.eq("messageStatus", read));
		getByStatus(dc, DataStatus.USING);
		List<Message> result = findByCriteria(dc);
		return result;
	}

	public List<Message> getReceivedMessage(Long userId) {
		DetachedCriteria dc = DetachedCriteria.forClass(Message.class);
		dc.add(Restrictions.eq("from.id", userId));
		getByStatus(dc, DataStatus.USING);
		List<Message> result = findByCriteria(dc);
		return result;
	}

	public List<Message> getSentMessage(Long userId) {
		DetachedCriteria dc = DetachedCriteria.forClass(Message.class);
		dc.add(Restrictions.eq("to.id", userId));
		getByStatus(dc, DataStatus.USING);
		List<Message> result = findByCriteria(dc);
		return result;
	}

}
